import BaseInput from "@/components/base/Input"
import { Props } from "@/components/form_item/formItemConfig"
import styled from "@emotion/styled"
import { memo } from "react"
import { useFormContext } from "react-hook-form"
import Svg from "../base/Svg"

const CheckboxInput = memo(
    ({ fieldName, required, className = "", style, initValue = false, isShowTip }: Props) => {
        const { register, getValues, setValue } = useFormContext()
        const value = getValues(fieldName) || initValue
        return (
            <Checkbox className={className}>
                <Check
                    style={style}
                    id={"CheckboxId-" + fieldName}
                    type="checkbox"
                    defaultChecked={value as boolean}
                    {...register(fieldName, {
                        required: {
                            message: `${fieldName} is required`,
                            value: required ?? true,
                        },
                        value: initValue,
                    })}
                    onChange={(e) => {
                        setValue(fieldName, e.target.checked, {
                            shouldValidate: true,
                            shouldDirty: true
                        })
                    }}
                />
                <Label htmlFor={"CheckboxId-" + fieldName}>
                    <span className={"checkbox_false"}>
                        <Svg src={"login/checkbox_false.svg"} />
                    </span>
                    <span className={"checkbox_true"}>
                        <Svg src={"login/checkbox_true.svg"} />
                    </span>
                </Label>
                {isShowTip && (
                    <Tip>
                        <TipSpan>
                            You need to agree to the Terms of Use and Privacy Policy to login
                            Filbet.
                        </TipSpan>
                        <TipLabel htmlFor={"CheckboxId-" + fieldName}>I agree</TipLabel>
                    </Tip>
                )}
            </Checkbox>
        )
    },
)

export default CheckboxInput

const Checkbox = styled.div`
    position: relative;
    margin-right: 5rem;
`

const Check = styled(BaseInput)`
    width: 20rem;
    height: 20rem;
    display: none;

    & + label > span.checkbox_false {
        display: inline-block;
    }

    &:checked + label > span.checkbox_true {
        display: inline-block;
    }

    &:checked + label > span.checkbox_false {
        display: none;
    }

    & ~ div {
        display: block;
    }

    &:checked ~ div {
        display: none;
    }
`

const Label = styled.label`
    width: 20rem;
    height: 20rem;
    display: block;
    & > span {
        line-height: normal;
        text-align: center;
    }
    & > span.checkbox_false,
    & > span.checkbox_true {
        display: none;
    }
`

const Tip = styled.div`
    position: absolute;
    left: 0;
    top: 30rem;
    border-radius: 5rem;
    padding: 10rem;
    height: 50rem;
    width: calc(100vw - 48rem);
    background: #0e0f1be5;
    z-index: 2;
    &::before {
        content: "";
        position: absolute;
        display: block;
        top: calc(-16rem + 1px);
        left: 3rem;
        width: 0;
        height: 0;
        border: 8rem solid;
        border-color: transparent transparent #0e0f1be5;
    }
`

const TipSpan = styled.span`
    color: #ffffffbf;
    font-family: "Gilroy", sans-serif;
    font-weight: 400;
    font-size: 12rem;
    line-height: 16rem;
    letter-spacing: 0;
    width: 247rem;
    height: 32rem;
    display: inline-block;
`

const TipLabel = styled.label`
    display: inline-block;
    background: linear-gradient(180deg, #fb517b 0%, #fb2d60 100%);
    outline: none;
    border: none;
    color: #fffffff2;
    font-family: "Gilroy", sans-serif;
    font-weight: 500;
    font-size: 12rem;
    line-height: 24rem;
    letter-spacing: 0;
    text-align: center;
    width: 72rem;
    height: 24rem;
    top: 14rem;
    right: 8rem;
    border-radius: 12rem;
    position: absolute;
`
